package cn.south.campus.business.service.impl;

import cn.south.campus.business.mapper.ExpressTakeDetailMapper;
import cn.south.campus.business.model.entity.ExpressTakeDetail;
import cn.south.campus.business.model.vo.ExpressTakeDetailVo;
import cn.south.campus.business.service.ExpressTakeDetailService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * <p>
 * 快递代取明细 服务实现类
 * </p>
 *
 * @author chow
 * @since 2024-12-13
 */
@Service
@AllArgsConstructor
public class ExpressTakeDetailServiceImpl extends ServiceImpl<ExpressTakeDetailMapper, ExpressTakeDetail> implements ExpressTakeDetailService {

    private final ExpressTakeDetailMapper expressTakeDetailMapper;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void batchInsertDetail(List<ExpressTakeDetailVo> expressTakeDetails) {
        List<ExpressTakeDetail> detailList = expressTakeDetails.stream()
                .map(ExpressTakeDetailVo::convert)
                .filter(Objects::nonNull)
                .collect(Collectors.toList());
        if (CollectionUtils.isEmpty(detailList)) {
            return;
        }
        saveBatch(detailList);
    }

    @Override
    public void delByTakeId(Long takeId) {
        LambdaQueryWrapper<ExpressTakeDetail> wrapper = new LambdaQueryWrapper<ExpressTakeDetail>()
                .eq(ExpressTakeDetail::getExpressTakeId, takeId);
        expressTakeDetailMapper.delete(wrapper);
    }

    @Override
    public List<ExpressTakeDetailVo> selectByTakeId(Long takeId) {
        LambdaQueryWrapper<ExpressTakeDetail> wrapper = new LambdaQueryWrapper<ExpressTakeDetail>()
                .eq(ExpressTakeDetail::getExpressTakeId, takeId);
        List<ExpressTakeDetail> detailList = expressTakeDetailMapper.selectList(wrapper);
        if (CollectionUtils.isEmpty(detailList)) {
            return null;
        }
        return detailList.stream().map(ExpressTakeDetailVo::convert).collect(Collectors.toList());
    }
}
